﻿/*

<javascriptresource>
<name>Test Layer Effects...</name>
<category>JSON Action Manager Tests</category>
</javascriptresource>

*/

//------------------------------------------------------------------------------
// File: Test Layer Effects.js
// Version: 3.3
// Release Date: 2012-10-21
// Copyright: © 2011-2012 Michel MARIANI <http://www.tonton-pixel.com/blog/>
// Licence: GPL <http://www.gnu.org/licenses/gpl.html>
//------------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

//@includepath "~/JSON Action Manager/"
//@include "jamEngine.jsxinc"
//@include "jamHelpers.jsxinc"
//@include "jamUtils.jsxinc"

//------------------------------------------------------------------------------

// Used to locate the preset files (custom shapes, contours, styles...)
Folder.current = new Folder ("~/JSON Action Manager/tests/resources/");

//------------------------------------------------------------------------------

var operations =
[
    "Set Layer Effects",
    "Apply Layer Style"
];
var desiredOperation = operations[0];
var allOperations = "*All*";

//------------------------------------------------------------------------------

var docWidth = 800;
var docHeight = 800;
var docResolution = 72;
var text = "X";

//------------------------------------------------------------------------------

function getParameters ()
{
    var dialogWidth = 200;
    var dialogHeight = 300; // Dynamically adjusted anyway
    var dialogPadding = [ 15, 15 ];
    var panelPadding = [ 15, 15 ];
    var buttonWidth = 80;
    var buttonHeight = 20;
    var buttonGap = 5;
    var extraGap = (parseFloat (app.version) > 8) ? 0 : 12;
    var dlg = new Window ('dialog', "Test Layer Effects", [ 0, 0, dialogWidth, dialogHeight ]);
    dlg.btnPnl = dlg.add ('panel', [ dialogPadding[0], dialogPadding[1], dialogWidth - dialogPadding[0], dialogHeight - dialogPadding[1] ], 'Operation');
    dlg.btnPnl.horizBtn = [];
    var y = panelPadding[1] + extraGap;
    var count = operations.length;
    for (var i = 0; i < count; i++)
    {
        dlg.btnPnl.horizBtn.push (dlg.btnPnl.add ('radiobutton', [ panelPadding[0], y, (dlg.btnPnl.bounds.right - dlg.btnPnl.bounds.left) - panelPadding[0], y + buttonHeight ], operations[i]));
        y += buttonHeight + buttonGap;
    }
    dlg.btnPnl.horizBtn.push (dlg.btnPnl.add ('radiobutton', [ panelPadding[0], y, (dlg.btnPnl.bounds.right - dlg.btnPnl.bounds.left) - panelPadding[0], y + buttonHeight ], allOperations));
    y += buttonHeight + buttonGap;
    var count = dlg.btnPnl.horizBtn.length;
    for (var i = 0; i < count; i++)
    {
        var radbut = dlg.btnPnl.horizBtn[i];
        radbut.value = (radbut.text === desiredOperation);
        radbut.onClick = function () { desiredOperation = this.text; };
    }
    dlg.btnPnl.bounds.bottom = dlg.btnPnl.bounds.top + y + panelPadding[1];
    dlg.bounds.bottom = dlg.bounds.top + dialogPadding[1] + (dlg.btnPnl.bounds.bottom - dlg.btnPnl.bounds.top) + dialogPadding[1] + buttonHeight + dialogPadding[1];
    dlg.cancelBtn = dlg.add ('button', [ dialogPadding[0], dlg.bounds.bottom - dialogPadding[1] - buttonHeight, dialogPadding[0] + buttonWidth, dlg.bounds.bottom - dialogPadding[1] ], 'Cancel', { name: "cancel" });
    dlg.cancelBtn.onClick = function () { this.parent.close (false); };
    dlg.OKBtn = dlg.add ('button', [ dlg.bounds.right - dialogPadding[0] - buttonWidth, dlg.bounds.bottom - dialogPadding[1] - buttonHeight, dlg.bounds.right - dialogPadding[0], dlg.bounds.bottom - dialogPadding[1] ], 'OK', { name: "ok" });
    dlg.OKBtn.onClick = function () { this.parent.close (true); };
    dlg.center ();
    return dlg.show ();
}

//------------------------------------------------------------------------------

function processOperation (desiredOperation)
{
    jamEngine.jsonPlay
    (
        "make",
        {
            "new":
            {
                "<object>":
                {
                    "document":
                    {
                        "name": { "<string>": "Test Layer Effects | " + desiredOperation },
                        "mode": { "<class>": "RGBColorMode" },
                        "width": { "<unitDouble>": { "distanceUnit": jamUtils.toDistanceUnit (docWidth, docResolution) } },
                        "height": { "<unitDouble>": { "distanceUnit": jamUtils.toDistanceUnit (docHeight, docResolution) } },
                        "resolution": { "<unitDouble>": { "densityUnit": docResolution } },
                        "depth": { "<integer>": 8 },
                        "fill": { "<enumerated>": { "fill": "white" } },
                        "profile": { "<string>": "sRGB IEC61966-2.1" }
                    }
                }
            }
        }
    );
    if (desiredOperation === "Apply Layer Style")
    {
        jamEngine.jsonPlay ("invert");
    }
    if ((desiredOperation !== "Apply Layer Style") && jamUtils.fontExists ("AppleGaramond-Light"))
    {
        jamEngine.jsonPlay
        (
            "make",
            {
                "target": { "<reference>": [ { "textLayer": { "<class>": null } } ] },
                "using":
                {
                    "<object>":
                    {
                        "textLayer":
                        {
                            "textKey": { "<string>": text },
                            "textClickPoint": jamHelpers.toPointObject ([ [ 50.75, 92.75 ], "percentUnit" ]),
                            "antiAlias": { "<enumerated>": { "antiAliasType": "antiAliasCrisp" } },
                            "textStyleRange":
                            {
                                "<list>":
                                [
                                    {
                                        "<object>":
                                        {
                                            "textStyleRange":
                                            {
                                                "from": { "<integer>": 0 },
                                                "to": { "<integer>": text.length },
                                                "textStyle":
                                                {
                                                    "<object>":
                                                    {
                                                        "textStyle":
                                                        {
                                                            "fontPostScriptName": { "<string>": "AppleGaramond-Light" },
                                                            "size": { "<unitDouble>": { "pixelsUnit": 1024.0 } },
                                                            "color": jamHelpers.toColorObject ([ "grayscale", (desiredOperation === "Apply Layer Style") ? 0 : 100 ])
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                ]
                            },
                            "paragraphStyleRange":
                            {
                                "<list>":
                                [
                                    {
                                        "<object>":
                                        {
                                            "paragraphStyleRange":
                                            {
                                                "from": { "<integer>": 0 },
                                                "to": { "<integer>": text.length },
                                                "paragraphStyle":
                                                {
                                                    "<object>":
                                                    {
                                                        "paragraphStyle":
                                                        {
                                                            "alignment": { "<enumerated>": { "alignmentType": "center" } }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        );
    }
    else
    {
        var shapeName = "Logo X-Aqua in Apple Garamond Light";
        var shapePresetPath = "Logo-X-Aqua.csh";
        jamUtils.loadPreset ("customShape", shapeName, shapePresetPath);
        jamEngine.jsonPlay
        (
            "make",
            {
                "target": { "<reference>": [ { "contentLayer": { "<class>": null } } ] },
                "using":
                {
                    "<object>":
                    {
                        "contentLayer":
                        {
                            "type":
                            {
                                "<object>":
                                {
                                    "solidColorLayer":
                                    {
                                        "color": jamHelpers.toColorObject ([ "grayscale", (desiredOperation === "Apply Layer Style") ? 0 : 100 ])
                                    }
                                }
                            },
                            "shape": jamHelpers.toCustomShapeObject ([ [ shapeName, 136, 97, 675.5, 742 ], "pixelsUnit" ])
                        }
                    }
                }
            }
        );
    }
    jamUtils.loadPreset ("shapingCurve", "Half Round", "Default-Contours.shc");
    switch (desiredOperation)
    {
        case "Set Layer Effects":
            jamEngine.jsonPlay
            (
                "set",
                {
                    "target": { "<reference>": [ { "layer": { "<enumerated>": { "ordinal": "targetEnum" } } } ] },
                    "to":
                    {
                        "<object>":
                        {
                            "layer":
                            {
                                "fillOpacity": { "<unitDouble>": { "percentUnit": 70 } },
                                "blendInterior": { "<boolean>": true },
                                "layerEffects":
                                {
                                    "<object>":
                                    {
                                        "layerEffects":
                                        {
                                            "scale": { "<unitDouble>": { "percentUnit": 100.0 } },
                                            "dropShadow":
                                            {
                                                "<object>":
                                                {
                                                    "dropShadow":
                                                    {
                                                        "enabled": { "<boolean>": true },
                                                        "mode": { "<enumerated>": { "blendMode": "multiply" } },
                                                        "color": jamHelpers.toColorObject ([ "RGBColor", [ 0, 0, 0 ] ]),
                                                        "opacity": { "<unitDouble>": { "percentUnit": 70 } },
                                                        "useGlobalAngle": { "<boolean>": false },
                                                        "localLightingAngle": { "<unitDouble>": { "angleUnit": 90 } },
                                                        "distance": { "<unitDouble>": { "pixelsUnit": 6 } },
                                                        "chokeMatte": { "<unitDouble>": { "percentUnit": 0 } },
                                                        "blur": { "<unitDouble>": { "pixelsUnit": 4 } },
                                                        "transparencyShape": { "<object>": { "shapingCurve": { "name": { "<string>": "Linear" } } } },
                                                        "layerConceals": { "<boolean>": true }
                                                    }
                                                }
                                            },
                                            "innerShadow":
                                            {
                                                "<object>":
                                                {
                                                    "innerShadow":
                                                    {
                                                        "enabled": { "<boolean>": true },
                                                        "mode": { "<enumerated>": { "blendMode": "multiply" } },
                                                        "color": jamHelpers.toColorObject ([ "RGBColor", [ 0, 0, 0 ] ]),
                                                        "opacity": { "<unitDouble>": { "percentUnit": 70 } },
                                                        "useGlobalAngle": { "<boolean>": false },
                                                        "localLightingAngle": { "<unitDouble>": { "angleUnit": 90 } },
                                                        "distance": { "<unitDouble>": { "pixelsUnit": 10 } },
                                                        "chokeMatte": { "<unitDouble>": { "percentUnit": 15 } },
                                                        "blur": { "<unitDouble>": { "pixelsUnit": 20 } },
                                                        "transparencyShape": { "<object>": { "shapingCurve": { "name": { "<string>": "Linear" } } } },
                                                    }
                                                }
                                            },
                                            "innerGlow":
                                            {
                                                "<object>":
                                                {
                                                    "innerGlow":
                                                    {
                                                        "enabled": { "<boolean>": true },
                                                        "mode": { "<enumerated>": { "blendMode": "linearDodge" } },
                                                        "opacity": { "<unitDouble>": { "percentUnit": 31 } },
                                                        "color": jamHelpers.toColorObject ([ "RGBColor", [ 255, 246, 168 ] ]),
                                                        "glowTechnique": { "<enumerated>": { "matteTechnique": "preciseMatte" } },
                                                        "innerGlowSource": { "<enumerated>": { "innerGlowSourceType": "centerGlow" } },
                                                        "chokeMatte": { "<unitDouble>": { "percentUnit": 0 } },
                                                        "blur": { "<unitDouble>": { "pixelsUnit": 20 } },
                                                        "transparencyShape": { "<object>": { "shapingCurve": { "name": { "<string>": "Half Round" } } } },
                                                        "antiAlias": { "<boolean>": true },
                                                        "inputRange": { "<unitDouble>": { "percentUnit": 43 } },
                                                        "shadingNoise": { "<unitDouble>": { "percentUnit": 0 } }
                                                    }
                                                }
                                            },
                                            "bevelEmboss":
                                            {
                                                "<object>":
                                                {
                                                    "bevelEmboss":
                                                    {
                                                        "enabled": { "<boolean>": true },
                                                        "bevelStyle": { "<enumerated>": { "bevelEmbossStyle": "innerBevel" } },
                                                        "bevelTechnique": { "<enumerated>": { "bevelTechnique": "softMatte" } },
                                                        "strengthRatio": { "<unitDouble>": { "percentUnit": 80 } },
                                                        "bevelDirection": { "<enumerated>": { "bevelEmbossStampStyle": "stampIn" } },
                                                        "blur": { "<unitDouble>": { "pixelsUnit": 16 } },
                                                        "softness": { "<unitDouble>": { "pixelsUnit": 0 } },
                                                        "useGlobalAngle": { "<boolean>": false },
                                                        "localLightingAngle": { "<unitDouble>": { "angleUnit": 90 } },
                                                        "localLightingAltitude": { "<unitDouble>": { "angleUnit": 75 } },
                                                        "transparencyShape": { "<object>": { "shapingCurve": { "name": { "<string>": "Linear" } } } },
                                                        "highlightMode": { "<enumerated>": { "blendMode": "screen" } },
                                                        "highlightColor": jamHelpers.toColorObject ([ "RGBColor", [ 255, 255, 255 ] ]),
                                                        "highlightOpacity": { "<unitDouble>": { "percentUnit": 100 } },
                                                        "shadowMode": { "<enumerated>": { "blendMode": "multiply" } },
                                                        "shadowColor": jamHelpers.toColorObject ([ "RGBColor", [ 0, 0, 0 ] ]),
                                                        "shadowOpacity": { "<unitDouble>": { "percentUnit": 100 } },
                                                        "useShape": { "<boolean>": true },
                                                        "mappingShape": { "<object>": { "shapingCurve": { "name": { "<string>": "Half Round" } } } },
                                                        "antiAlias": { "<boolean>": true },
                                                        "inputRange": { "<unitDouble>": { "percentUnit": 70 } },
                                                    }
                                                }
                                            },
                                            "solidFill":
                                            {
                                                "<object>":
                                                {
                                                    "solidFill":
                                                    {
                                                        "enabled": { "<boolean>": true },
                                                        "color": jamHelpers.toColorObject ([ "RGBColor", [ 0, 102, 255 ] ])
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            );
            break;
        case "Apply Layer Style":
            jamUtils.loadPreset ("style", "Logo X-Aqua in Blue Glass (Button)", "Logo-X-Aqua.asl");
            jamEngine.jsonPlay
            (
                "applyStyle",
                {
                    "target": { "<reference>": [ { "style": { "<name>": "Logo X-Aqua in Blue Glass (Button)" } } ] }
                }
            );
            break;
    }
    jamEngine.jsonPlay
    (
        "make",
        {
            "target": { "<reference>": [ { "adjustmentLayer": { "<class>": null } } ] },
            "using":
            {
                "<object>":
                {
                    "adjustmentLayer":
                    {
                        "name": { "<string>": "Neutral Overlay" },
                        "mode": { "<enumerated>": { "blendMode": "overlay" } },
                        "type": { "<object>": { "brightnessContrast": null } }
                    }
                }
            }
        }
    );
}

//------------------------------------------------------------------------------

if (getParameters ())
{
    if (desiredOperation === allOperations)
    {
        for (var i = 0; i < operations.length; i++)
        {
            processOperation (operations[i]);
        }
    }
    else
    {
        processOperation (desiredOperation);
    }
}

//------------------------------------------------------------------------------

